package com.fz.us.admin.base.utils;

import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ZhuChunXiao on 2017/5/2.
 */
public class EntityUtil {
    private EntityUtil(){}

    public static Object getFieldValueByName(String fieldName,Object o) {
        try {
            boolean flagGet = false;
            boolean flagIs = false;
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            String getter2 = "is" + firstLetter + fieldName.substring(1);
            Method[] methods = o.getClass().getMethods();
            Method m = null;
            Object value = null;
            for(Method method:methods){
                if(StringUtils.equals(method.getName(),getter)){
                    flagGet = true;
                    break;
                }else if(StringUtils.equals(method.getName(),getter2)){
                    flagIs = true;
                    break;
                }
            }
            if(flagGet){
                m  = o.getClass().getMethod(getter, new Class[]{});
                value = m.invoke(o, new Object[]{});
            }else if(flagIs){
                m  = o.getClass().getMethod(getter2, new Class[]{});
                value = m.invoke(o, new Object[]{});
            }
            return value;
        } catch (Exception e) {
            LogUtil.error(e.getMessage(), e);
            return null;
        }
    }

    /**
     * 获取属性名数组
     * */
    public static String[] getFiledName(Class o){
        Field[] fields=o.getDeclaredFields();
        String[] fieldNames=new String[fields.length];
        for(int i=0;i<fields.length;i++){
           // System.out.println(fields[i].getType());
            fieldNames[i]=fields[i].getName();
        }
        return fieldNames;
    }

    /**
     * 获取属性类型(type)，属性名(name)，属性值(value)的map组成的list
     * */
    public static List getFiledsInfo(Object o){
        Field[] fields=o.getClass().getDeclaredFields();
        String[] fieldNames=new String[fields.length];
        List list = new ArrayList();
        Map infoMap=null;
        for(int i=0;i<fields.length;i++){
            infoMap = new HashMap();
            infoMap.put("type", fields[i].getType().toString());
            infoMap.put("name", fields[i].getName());
            infoMap.put("value", getFieldValueByName(fields[i].getName(), o));
            list.add(infoMap);
        }
        return list;
    }

    /**
     * 获取对象的所有属性值，返回一个对象数组
     * */
    public static Object[] getFiledValues(Object o){
        String[] fieldNames=getFiledName(o.getClass());
        Object[] value=new Object[fieldNames.length];
        for(int i=0;i<fieldNames.length;i++){
            value[i]= getFieldValueByName(fieldNames[i], o);
        }
        return value;
    }
}
